package cn.swu.Waimai


import android.Manifest
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import cn.swu.Waimai.R
import cn.swu.Waimai.ui.activity.BaseActivity
import cn.swu.Waimai.ui.fragment.IndexFragment
import cn.swu.Waimai.ui.fragment.MeFragment
import cn.swu.Waimai.ui.fragment.MenuFragment
import cn.swu.Waimai.Util.ActivityCollector
import com.google.android.material.bottomnavigation.BottomNavigationView
import java.util.*


class MainActivity : BaseActivity() {

    private lateinit var indexFrag: Fragment
    private lateinit var menuFrag: Fragment
    private lateinit var meFrag: Fragment
    private lateinit var bottomNav: BottomNavigationView
    //    private lateinit var fragShoppingCart:Fragment
//    private lateinit var fragMe:Fragment
    private lateinit var fragments: Array<Fragment>

    //用于记录上个选择的Fragment
    companion object{
        var lastFragment = 0
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initFragment()
        requestPermission()
        Log.d("TAG", "hello")
    }

    //退出软件
    override fun onBackPressed() {
        AlertDialog.Builder(this).setTitle("退出")
            .setMessage("确定要退出吗")
            .setPositiveButton("确定", DialogInterface.OnClickListener { _, _ ->
                ActivityCollector.finishAll()
            }).setNegativeButton("取消", null).show()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    private fun initFragment() {
        bottomNav = findViewById(R.id.main_bottom_nav)
        indexFrag = IndexFragment()
        menuFrag = MenuFragment()
        meFrag = MeFragment()
        fragments = arrayOf(indexFrag, menuFrag, meFrag)

        //底部选择bar
        when (lastFragment) {
            0 -> supportFragmentManager.beginTransaction()
                .replace(R.id.main_above_container, indexFrag).show(indexFrag).commit()

            1 -> supportFragmentManager.beginTransaction()
                .replace(R.id.main_above_container, menuFrag).show(menuFrag).commit()

            2 -> supportFragmentManager.beginTransaction()
                .replace(R.id.main_above_container, meFrag).show(meFrag).commit()

        }

        bottomNav.setOnNavigationItemSelectedListener(BottomNavigationView.OnNavigationItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.bottom_main -> {
                    if (lastFragment != 0) {
                        switchFragment(lastFragment, 0)
                        lastFragment = 0
                    }
                    return@OnNavigationItemSelectedListener true
                }
                R.id.bottom_menu -> {
                    if (lastFragment != 1) {
                        switchFragment(lastFragment, 1)
                        lastFragment = 1
                    }
                    return@OnNavigationItemSelectedListener true
                }

                R.id.bottom_me -> {
                    if (lastFragment != 2) {
                        switchFragment(lastFragment, 2)
                        lastFragment = 2
                    }
                    return@OnNavigationItemSelectedListener true
                }

            }
            true
        })
        bottomNav.setOnNavigationItemReselectedListener {
            Log.d("TAG", "1")
            when(it.itemId){
                R.id.bottom_main -> {
                    if (lastFragment != 0) {
                        switchFragment(lastFragment, 0)
                        lastFragment = 0
                    }

                }
                R.id.bottom_menu -> {
                    if (lastFragment != 1) {
                        switchFragment(lastFragment, 1)
                        lastFragment = 1
                    }

                }
                R.id.bottom_me -> {
                    if (lastFragment != 2) {
                        switchFragment(lastFragment, 2)
                        lastFragment = 2
                    }

                }
            }


        }

    }
    private fun switchFragment(lastIndex: Int, index: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.hide(fragments[lastIndex])
        if (!fragments[index].isAdded) {
            transaction.add(R.id.main_above_container, fragments[index])
        }
        transaction.show(fragments[index]).commitAllowingStateLoss()
    }

    private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            val permissionsList = ArrayList<String>()
            val permissions = arrayOf(
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_WIFI_STATE)
            for (perm in permissions) {
                if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(perm)) {
                    permissionsList.add(perm)
                }
            }
            if (!permissionsList.isEmpty()) {
                val strings = arrayOfNulls<String>(permissionsList.size)
                requestPermissions(permissionsList.toArray(strings), 0)
            }
        }
    }


}